#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "basics.h"
#include "l5image.h"
#include "l7image.h"
#include "dialogspectralsignature.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDialog>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);
    this->satImage = NULL;
    this->scaleFactor = 0.5f;

    //se conectan las acciones con los elementos y eventos
    connect(this->ui->actionSalir,SIGNAL(triggered()),this,SLOT(close()));
    connect(this->ui->actionLansat_5,SIGNAL(triggered()),this,SLOT(openL5File()));
    connect(this->ui->actionLansat_7,SIGNAL(triggered()),this,SLOT(openL7File()));
    connect(this->ui->ImageFrame,SIGNAL(pointClicked(QPoint)),this,SLOT(spectralSignature(QPoint)));
    connect(this->ui->actionSeleccion,SIGNAL(triggered()),this,SLOT(doGrowing()));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete satImage;
}

void  MainWindow::doGrowing()
{
    if (this->satImage)
    {
        QSize size(this->satImage->getWidth() * scaleFactor, this->satImage->getHeight() * scaleFactor);
        //this->satImage->region
        QImage *image; // = new QImage(this->satImage->doRegionGrowing(this->ui->sbGrayBand->value(), size));
        this->ui->ImageFrame->setImage(image);
        QImage map = image->scaled(this->ui->miniMap->size());
        this->ui->miniMap->setImage(map);
    }
    else
    {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::information(this, tr("QMessageBox::information()"), "No Ha sido seleccionada ninguna imagen");
    }

}

void MainWindow::openL5File()
{
    QString fileName = QFileDialog::getOpenFileName(this,tr("Seleccione el header de LANSAT 5"),tr("/home"),tr("LANSAT 5 (*.dat)"));
    if (this->satImage) delete (this->satImage);
    this->satImage = new L5Image();
    if (this->satImage->readImage(QVector<QString>() << fileName))
        this->setWindowTitle("Imagen LANSAT 5: " + fileName);
}


//Si no hay diferencia seria el mismo metodo, cambiarian los mensajes entre estos nada mas

void MainWindow::openL7File()
{

    QString fileName1 = QFileDialog::getOpenFileName(this,tr("Seleccione el header de LANSAT 7"),tr("/home"),tr("LANSAT 7 (*.FST)"));
    // Por que LANSAT 7 tiene dos headers!????

    //QString fileName2 = QFileDialog::getOpenFileName(this,tr("Abrir imagen Lansat 7 (header 2)..."),tr("/home"),tr("LANSAT 7 (*.FST)"));
    if (this->satImage) delete (this->satImage);
    this->satImage = new L7Image();
    if (this->satImage->readImage(QVector<QString>() << fileName1)) //<< fileName2))
        this->setWindowTitle("Imagen LANSAT 7: " + fileName1);
}

void MainWindow::openSacCFile()
{
}

void MainWindow::on_pbSeeFalseColor_clicked()
{
    if (this->satImage)
    {
        QSize size(this->satImage->getWidth() * scaleFactor, this->satImage->getHeight() * scaleFactor);
        QImage *image = new QImage(this->satImage->getFalseColorPreviewOf(this->ui->sbRedBand->value(),
                                                                          this->ui->sbGreenBand->value(),
                                                                          this->ui->sbBlueBand->value(),size));
        this->ui->ImageFrame->setImage(image);
        QImage map = image->scaled(this->ui->miniMap->size());
        this->ui->miniMap->setImage(map);
    }
    else
    {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::information(this, tr("QMessageBox::information()"), "No Ha sido seleccionada ninguna imagen");
    }
}

void MainWindow::on_pbSeeGrayColor_clicked()
{
    if (this->satImage)
    {
        QSize size(this->satImage->getWidth() * scaleFactor, this->satImage->getHeight() * scaleFactor);
        QImage *image = new QImage(this->satImage->getGrayPreviewOf(this->ui->sbGrayBand->value(), size));
        this->ui->ImageFrame->setImage(image);
        QImage map = image->scaled(this->ui->miniMap->size());
        this->ui->miniMap->setImage(map);
    }
    else
    {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::information(this, tr("QMessageBox::information()"), "No Ha sido seleccionada ninguna imagen");
    }
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    this->ui->porcent->setText(QVariant(value * 10).toString());
    this->scaleFactor = (float) this->ui->horizontalSlider->value() / 10.0f;
}

void MainWindow::spectralSignature(QPoint point)
{
    if (this->satImage)
    {
        QSize size(this->satImage->getWidth() * scaleFactor, this->satImage->getHeight() * scaleFactor);
        QVector<int> spectralSignature = this->satImage->getSpectralSignatureOf(point,size);
        DialogSpectralSignature *dialog = new DialogSpectralSignature(spectralSignature,this);
        dialog->show();
    }
}
